package com.liesheng.haylou.net;

import org.apache.http.conn.ssl.SSLSocketFactory;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Date:2019/11/6
 * <p>
 * Author:wuzhiguang
 * <p>
 * Description:
 */
public class RetrofitFactory {
    private static HttpService httpService;

    private static final String TAG = RetrofitFactory.class.getSimpleName();

    private static OkHttpClient getOkHttpClient() {

        X509TrustManager x509TrustManager = new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

            }

            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        };

        SSLContext sslContext = null;
        try {
            sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[]{x509TrustManager}, new SecureRandom());
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        // Retrofit是基于OkHttpClient的，可以创建一个OkHttpClient进行一些配置
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(new MyInterceptor().logInterceptor)
                .addInterceptor(new MyInterceptor().headInterceptor)
                .retryOnConnectionFailure(true) //设置网络连接失败时自动重试
                .connectTimeout(15, TimeUnit.SECONDS)
                .readTimeout(15, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .sslSocketFactory(new MySslSocketFactory(sslContext.getSocketFactory()), x509TrustManager) //设置https访问(验证证书)
                .hostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
                .build();
        return httpClient;
    }


    public static HttpService getHttpService() {
        if (httpService == null) {
            synchronized (HttpService.class) {
                if (httpService == null) {
                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl(HttpUrl.BASE_URL)
                            .client(getOkHttpClient())
                            .addConverterFactory(GsonConverterFactory.create())
                            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                            .build();
                    httpService = retrofit.create(HttpService.class);
                }
            }
        }
        return httpService;
    }

}
